package com.pocketmenu.ui;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public abstract class ListViewItemOnTouchListener implements OnTouchListener {

	private int padding = 0;
    private int initialx = 0;
    private int currentx = 0;    
    private int previousAction = 0;
    
    public static final int ACTION_SWIPE_RIGHT = 0;
    public static final int ACTION_SWIPE_LEFT = 1;    
    
    private boolean enableSwipeRight = false;
    private boolean enableSwipeLeft = false;
    
	@Override
    public boolean onTouch(View v, MotionEvent event) {            	    
   	    
		int background = 0xFFFFFFFF;
        if ( event.getAction() == MotionEvent.ACTION_DOWN)
        {
            padding = 0;
            initialx = (int) event.getX();
            currentx = (int) event.getX();                     
        }
        else if ( event.getAction() == MotionEvent.ACTION_MOVE)
        {
            currentx = (int) event.getX();
            padding = currentx - initialx;
                                    
            if(padding > 0 && enableSwipeRight)
            {
            	if(padding < 20)            		
       	 			background = 0x44A9F5A9;
            	else if(padding < 40)            		
       	 			background = 0x88A9F5A9;
            	else if(padding < 60)            		
       	 			background = 0xBBA9F5A9;
            	else if(padding >= 60)            		
       	 			background = 0xFFA9F5A9;
            }
	        else if(padding < 0 && enableSwipeLeft)
	        {
	        	if(padding > -20)            		
       	 			background = 0x44ffbbb6;
            	else if(padding > -40)            		
       	 			background = 0x88ffbbb6;
            	else if(padding > -60)            		
       	 			background = 0xBBffbbb6;
            	else if(padding <= -60)            		
       	 			background = 0xFFffbbb6;
	        		       	 	
	        }
	        else
	        	padding = 0;
        }        
        else if ( event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
        {        	                        
            if(previousAction == MotionEvent.ACTION_DOWN)
            	onClick(v);
            else if(previousAction == MotionEvent.ACTION_MOVE)
            {
            	if(enableSwipeLeft || enableSwipeRight)
            	{
            		padding = currentx - initialx;
	                if(padding > 50 && enableSwipeRight)
	                	onSwipe(v, ACTION_SWIPE_RIGHT);
	                else if(padding < -50 && enableSwipeLeft)
	                	onSwipe(v, ACTION_SWIPE_LEFT);
            	}
            	
                padding = 0;
                initialx = 0;
                currentx = 0;
            }
        }
        
        if(padding == 0)
   	 		background = 0xFFFFFFFF;
        
        v.setPadding(padding, 0, 0, 0);  
        v.setBackgroundColor(background);
        
        previousAction = event.getAction();
        
        return true;
    }		
	
	
	public ListViewItemOnTouchListener(int... actions) {
		super();
		for(int action : actions)
		{
			if(action == ACTION_SWIPE_LEFT)
				enableSwipeLeft = true;
			else if(action == ACTION_SWIPE_RIGHT)
				enableSwipeRight = true;
		}
	}


	//Abstract methods
	public abstract void onClick(View v);
	public abstract void onSwipe(View v, int action);

}
